
  $(function(){
      function myCity(result){
          $('.dy-city').val(result.name)
      }
      var getLocal = new BMap.LocalCity();
      getLocal.get(myCity);


      $('.pop-dy .sel.xj-car-brand').on('change', function () {
          var pid = $(this).val();
          $('.sel.xj-car-brand').val(pid);
          console.log(pid == '')
          if (pid != '0' && !(pid=='')) {
              var tempHTML = []
              $.getJSON("/index.php/?m=Car&a=CarType&ParentID="+pid, function(json){
                  json = eval(json);
                  console.log(json)
                  if(json.status == 1){
                     var parentid = json.data[0].ClassID;
                     var parentName = json.data[0].ClassName;
                      for(var i=0;i<json.data.length;i++){
                          if(json.data[i].Depth == 2 && parentid == json.data[i].ParentID){
                              parentid = json.data[i].ClassID;
                              parentName = json.data[i].ClassName;
                              tempHTML.push('<optgroup label="'+parentName+'">')
                              continue;
                          }

                          if(json.data[i].Depth == 2 && parentid != json.data[i].ParentID ){
                              tempHTML.push('</optgroup>');
                              parentid = json.data[i].ClassID;
                              parentName = json.data[i].ClassName;
                              tempHTML.push('<optgroup label="'+parentName+'">');
                              continue;
                          }

                          if( json.data[i].Depth == 3){
                              tempHTML.push('<option value="'+json.data[i].ClassID+'"> '+json.data[i].ClassName+' </option>');
                              continue;
                          }

                          if (i == json.data.length-1){
                              tempHTML.push('<option value="'+json.data[i].ClassID+'"> '+json.data[i].ClassName+' </option>');
                              tempHTML.push('</optgroup>');
                              continue;
                          }
                      }

                      $('.sel.xj-cars').html( tempHTML.join("") );
                      $('.sel.xj-cars').trigger("change");

                  }else{
                     alert(json.info);
                  }
              });
          } else {
              $('.sel.xj-cars').html( '  <option value="0">先选品牌</option>' );
              $('.sel.xj-cars').val(0);
              $('.sel.xj-car').val(0);
          }
          checkFormXj();
      });

      $('.pop-dy .sel.xj-cars').on('change', function () {
          var pid = $(this).val();
          $('.sel.xj-cars').val(pid);
          if (pid != '0') {
              checkFormXj();
          } else {
              $('.sel.xj-car').html('<option value="0">车型</option>');
          }
      });

      $('.pop-dy .dy-mobile').on('blur', function () {
          checkFormXj();
      });

      $('.pop-dy .dy-name').on('blur', function () {
          checkFormXj();
      });

      $('.pop-dy .close,.art_title_dy,.dy-btn,.xj-btn,.megaphone-icon,.btn-low,.buy-btn').on('click', function (e) {
          e.stopPropagation();
          e.preventDefault();

          $('.pop-dy').toggleClass('none');
          checkFormXj();
      });

      $('.pop-dy .btn').on('click', function() {
          if (!checkTel($('.dy-mobile').val())) {
              alert('手机号填写有误');
              return;
          }
          var i = checkFormXj().num;
          if (i > 0) {
              alert('还有'+i+'个信息需要填写哦');
              return;
          }
          console.log(i)
          //todo  submit
          $.ajax({
              type:"POST",
              url:"/index.php/?m=Car&a=AddShiJia",
              data:{tel:$('.dy-mobile').val(), carPinpai: $('.xj-car-brand').val(), carType: $('.xj-cars').val(),name:$('.dy-name').val(),city:$('.dy-city').val(),source1:$('.dy-source1').val()},
              datatype: "json",
              success:function(data){
                  var jsonObj=eval(data);
                  if (jsonObj.status == 1) {
                      $('.pop-dy').toggleClass('none');
                      alert(jsonObj.info);
                  } else {
                      alert(jsonObj.info);
                  }
              },
              error: function(){
                  alert("无法连接到服务器！");
              }
           });
      });


      $('.pop-plu .sel.xj-car-brand,.plu .sel.xj-car-brand').on('change', function () {
          var pid = $(this).val();
          $('.sel.xj-car-brand').val(pid);
          console.log(pid == '')
          if (pid != '0' && !(pid=='')) {
              var tempHTML = [];
              $.getJSON("/index.php/?m=Car&a=CarType&ParentID="+pid, function(json){
                  json = eval(json);
                  if(json.status == 1){
                      var parentid = json.data[0].ClassID;
                      var parentName = json.data[0].ClassName;
                      for(var i=0;i<json.data.length;i++){
                          if(json.data[i].Depth == 2 && parentid == json.data[i].ParentID){
                              parentid = json.data[i].ClassID;
                              parentName = json.data[i].ClassName;
                              tempHTML.push('<optgroup label="'+parentName+'">')
                              continue;
                          }

                          if(json.data[i].Depth == 2 && parentid != json.data[i].ParentID ){
                              tempHTML.push('</optgroup>');
                              parentid = json.data[i].ClassID;
                              parentName = json.data[i].ClassName;
                              tempHTML.push('<optgroup label="'+parentName+'">');
                              continue;
                          }

                          if( json.data[i].Depth == 3){
                              tempHTML.push('<option value="'+json.data[i].ClassID+'"> '+json.data[i].ClassName+' </option>');
                              continue;
                          }

                          if (i == json.data.length-1){
                              tempHTML.push('<option value="'+json.data[i].ClassID+'"> '+json.data[i].ClassName+' </option>');
                              tempHTML.push('</optgroup>');
                              continue;
                          }
                      }

                      $('.sel.xj-cars').html( tempHTML.join("") );
                      $('.sel.xj-cars').trigger("change");

                  }else{
                      alert(json.info);
                  }
              });
          } else {

              $('.sel.xj-cars').html( '  <option value="0">先选品牌</option>' );
              $('.sel.xj-cars').val(0);
              $('.sel.xj-car').val(0);
          }
          checkFormXj();
      });

      $('.pop-plu .sel.xj-cars,.plu .sel.xj-cars').on('change', function () {
          var pid = $(this).val();
          $('.sel.xj-cars').val(pid);
          if (pid != '0' && !(pid=='')) {
              var tempHTML = [];
              $.getJSON("/index.php/?m=Car&a=CarStyle&ParentID="+pid, function(json){
                  json = eval(json);
                  if(json.status == 1){
                      for(var i=0;i<json.data.length;i++){
                           tempHTML.push('<option value="'+json.data[i].ID+'"> '+json.data[i].Title+' </option>');
                      }
                      $('.sel.xj-car').html( tempHTML.join("") );
                      $('.sel.xj-car').trigger("change");
                  }else{
                      alert(json.info);
                  }
              });
          } else {
              $('.sel.xj-cars').html( '  <option value="0">先选品牌</option>' );
              $('.sel.xj-cars').val(0);
              $('.sel.xj-car').html('<option value="0">车型</option>');
              $('.sel.xj-car').val(0);
          }
          checkFormXj();
      });

      $('.pop-plu .sel.xj-car').on('change', function () {
          var pid = $(this).val();
          $('.sel.xj-car').val(pid);
          if (pid != '0') {
              checkFormXj();
          }
      });

      $('.pop-plu .dy-mobile').on('blur', function () {
          checkFormXj();
      });

      $('.pop-plu .dy-name').on('blur', function () {
          checkFormXj();
      });

      $('.pop-plu .close,.btn').on('click', function (e) {
          e.stopPropagation();
          e.preventDefault();

          $('.pop-plu').toggleClass('none');
          checkFormXj();
      });

      $('.pop-plu .btn').on('click', function() {
          if (!checkTel($('.dy-mobile').val())) {
              alert('手机号填写有误');
              return;
          }
          var i = checkFormXj().num;
          if (i > 0) {
              alert('还有'+i+'个信息需要填写哦');
              return;
          }
          $.ajax({
              type:"POST",
              url:"/index.php/?m=Car&a=AddShiJia",
              data:{
                  tel:$('.dy-mobile').val(),
                  carPinpai: $('.xj-car-brand').val(),
                  carType: $('.xj-cars').val(),
                  car:$('.xj-car').val(),
                  name:$('.dy-name').val(),
                  city:$('.dy-city').val(),
                  source1:$('.dy-source1').val()
              },
              datatype: "json",
              success:function(data){
                  var jsonObj=eval(data);
                  if (jsonObj.status == 1) {
                      $('.pop-dy').toggleClass('none');
                      alert(jsonObj.info);
                  } else {
                      alert(jsonObj.info);
                  }
              },
              error: function(){
                  alert("无法连接到服务器！");
              }
          });
      });

  });

  /**
   * 手机号码规范
   * @param tel
   * @returns {boolean}
   */
  function checkTel(tel) {
     var mobile = /^1[3|5|8]\d{9}$/ , phone = /^0\d{2,3}-?\d{7,8}$/;
     return mobile.test(tel) || phone.test(tel);
  }

  /**
   * 表格检查
   * @returns {{num: number, str: string}}
   */
  var checkFormXj = function () {
      var i = 0,
          str = '';
      if (!$('.pop-dy').hasClass('none')) {
          if ($('.pop-dy .sel.xj-car-brand').val() == 0) {
              i++;
              str = "请填写品牌";
          }

          if ($('.pop-dy .sel.xj-cars').val() == 0) {
              i++;
              str = "请填写车系";
          }
      }

      if (!$('.pop-plu').hasClass('none')) {
          if ($('.pop-plu .sel.xj-car-brand').val() == 0) {
              i++;
              str = "请填写品牌";
          }

          if ($('.pop-plu .sel.xj-cars').val() == 0) {
              i++;
              str = "请填写车系";
          }

          if ($('.pop-plu .sel.xj-car').val() == 0) {
              i++;
              str = "请填写车型";
          }
      }

      if ($('.dy-mobile').val() == '') {
          i++;
          str = "请填写手机";
      }

      if ($('.dy-name').val() == '') {
          i++;
          str = "请填写姓名";
      }


      $('.pop-plu .info, .pop-dy .info').html('还有'+i+'个信息需要填写哦~')
      return {num: i, str: str};
  };

